<template>
  <div class="container">
    <div class="app-container">
      <!-- 树形结构 -->
      <el-tree :expand-on-click-node="false" default-expand-all :data="departData" :props="defaultProps">
        <template v-slot="{data}">
          <el-row style="width:100%;height:40px" justify="space-between" type="flex" align="middle">
            <el-col>{{ data.name }} </el-col>
            <el-col :span="4">
              <span class="tree-manager">{{ data.managerName }} </span>
              <!-- 下拉菜单 -->
              <el-dropdown class="dropdown" @command="dropdownHandler($event,data.id)">
                <!-- 显示区域内容 -->
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <!-- 下拉菜单选项 -->
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>

      <!-- 添加子部门弹层 你这里正向传值  不就该传showDialog这个嘛 你的疑惑是什么  哈哈-->
      <addDept ref="addDept" :currentid="currentId" :showdialog.sync="dialogVisable" @updateDepartment="getDepartmentList" />
    </div>
  </div>
</template>
<script>
import { getDepartmentList, deleteDepartment } from '@/api/department'
import { transListToTreeData } from '@/utils/index'
import addDept from '@/views/department/components/addDept.vue'
export default {
  name: 'Department',
  components: { addDept },
  data() {
    return {
      currentId: null,
      departData: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      dialogVisable: false
    }
  },
  created() {
    this.getDepartmentList()
  },
  methods: {
    async getDepartmentList() {
      this.departData = transListToTreeData(await getDepartmentList(), 0)
    },
    async dropdownHandler(type, id) {
      if (type === 'add') {
        this.dialogVisable = true
        this.currentId = id
      } else if (type === 'edit') {
        this.dialogVisable = true
        this.currentId = id
        this.$nextTick(() => {
          this.$refs.addDept.getDepartmentDetail()
        })
      } else {
        this.$confirm(
          '您确认要删除该部门吗',
          '提示',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        )
          .then(async() => {
            await deleteDepartment(id)
            this.$message.success('删除成功')
            this.getDepartmentList()
          })
      }
    }

  }
}
</script>

<style scoped>
.app-container {
  padding: 30px 140px;
  font-size: 14px;
}
.tree-manager {
  display: inline-block;
  width: 50px;
  margin: 10px;

}
.dropdown {
  padding: 20px;
}
</style>>
